
let lrcHeight = 0; // 每句歌词滚动的高度
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    lyric:{
      type:String,
      value:""
    }
  },
  observers:{
    lyric(val){
      // console.log(val);
      this.parseLyric(val);
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    lrcList:[],
    nowlrcIndex:-1,//高亮歌词的索引
    scrollTop: 0
  },
  lifetimes:{
    ready(){
      // 手机按照750rpx划分
      wx.getSystemInfo({
        success: (result) => {
          //求出不同屏幕的1rpx是多少 再乘以 歌词的高度 height = 每句滚动的高度
          lrcHeight = result.screenWidth /750 *60
        },
      })
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    //联动歌词
    undate(currentTime){
      let lrcList = this.data.lrcList;
      // if(typeof lrcList[lrcList.length-1] != 'undefined'){
      //      if(currentTime > lrcList[lrcList.length-1].time){
      //         if(this.data.nowlrcIndex != -1){
      //           this.setData({
      //               nowPlayingIndex: -1,
      //               scrollTop: lrcList.length *lrcHeight
      //           })
      //         }
      // }
      // }
      for(let i = 0;i<lrcList.length;i++){
        if(currentTime <=lrcList[i].time){
            this.setData({
              nowlrcIndex: i-1,
              scrollTop: (i-1)*lrcHeight
            })
            break;
        }
      }
    },
    // 解析歌词
    parseLyric(val){
      // if(val.indexOf("\n")==-1){
      //   let reg=/(\[[\d\r]+:[\d\r]+\.[\d\r]+\])(.*?)\[/gi;
      //   val=val.replace(reg,"$1$2\\n");
      // }
      // console.log("geci===》",val)

      let line = val.split(/\n/)

      // console.log(line);
      let _lrcList = [];
      line.map(item=>{
        let time = item.match(/\[(\d{2,}):(\d{2})(?:\.(\d{2,3}))?]/g)
        if(time != null){
          let lrc = item.split(time)[1];
          let timeReg = time[0].match(/(\d{2,}):(\d{2})(?:\.(\d{2,3}))?/)
          //时间转为秒
          let time2Second = parseInt(timeReg[1])*60 + parseInt(timeReg[2])+parseInt(timeReg[3])/1000
          _lrcList.push({
            lrc,
            time:time2Second
          })
        }
      })
      this.setData({
        lrcList:_lrcList
      })
    }
  }
})
